<template>
	<div>
		<div style="justify-content: center;">
			<el-button type="primary" @click="re()" size="small" style="width: 200px;">返回</el-button>
		</div>
		<div style="width: 100%;">
			商品名称:<el-input v-model="book.name" type="text"></el-input>
		</div>
		<hr>
		<div style="width: 100%;">
			商品序号:<el-input v-model="book.sn" type="text"></el-input>
		</div>
		<div style="width: 100%;">
			商品价格：<el-input v-model="book.price" type="text"></el-input>
		</div>
		<div style="width: 100%;">
			商品厂商：<el-input v-model="book.maker" type="text"></el-input>
			<div style="width: 100%;">
				书籍作者：<el-input v-model="book.author" type="text"></el-input>
			</div>
			<div style="width: 100%;">
				出厂时间：<el-date-picker v-model="book.tmake" type="date" placeholder="选择日期"></el-date-picker>
			</div>
			<div style="width: 100%;">
				商品库存：<el-input v-model="book.stock" type="text"></el-input>
			</div>
			<div style="width: 100%;">
				商品类型：<el-cascader :options="booktype" v-model="book.typeid"></el-cascader>
			</div>
			<div style="width: 100%;">
				商品详情：<el-input v-model="book.info" type="text"></el-input>
			</div>
			<div style="width: 10vw; height: 10vw;">
				图片:<input type="file" ref="imgs" id="pic">
				<hr>
			</div>
			<div style="display: flex; justify-content: center;">
				<el-button type="primary" @click="add()" size="small" style="width: 200px;"> 添加</el-button>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'addShp',
		data() {
			return {
				book: {
					id:0
				},
				booktype: [],
			}
		},
		methods: {
			re() {
				this.$router.push({
					path: '/PC/ListPic',
				})
			},
			btn(id) {
				console.log(id);
			},
			goupdate(id) {
				this.act = 0;
				let token = window.localStorage.getItem("token")
				let _this = this;
				this.$get("/api/adm/book/findById/" + id)
					.then((resp) => {
						_this.book = resp.data.data;
						console.log(resp.data.data);
					})
			},
			add() {
				let fd = new FormData
				fd.append("id", this.book.id)
				console.log("书本ID");
				console.log(this.book.id);
				fd.append("name", this.book.name)
				fd.append("sn", this.book.sn)
				fd.append("price", this.book.price)
				fd.append("maker", this.book.maker)
				fd.append("author", this.book.author)
				fd.append("tmake", new Date(this.book.tmake).getTime())
				fd.append("stock", this.book.stock)
				console.log("fd===========");
				console.log(fd);
				let ip = this.booktype.length - 1
				let typeid = 0
				if (ip == 0) {
					typeid != this.booktype[ip]
				} else {
					typeid = this.booktype[ip]
				}
				console.log(typeid);
				fd.append("type.id", typeid.value)
				fd.append("info", this.book.info)
				console.log(fd);
				let pic = this.$refs.imgs.files[0]
				fd.append("mypic", pic)
				let _this = this;
				this.$postf("/api/adm/book/add", fd, ).then((resp) => {
					_this.book = resp.data.data;
					console.log(resp.data.data);
				})
				this.re()
			},
		},
		mounted() {
			this.$get('/api/adm/type/findAll2').then((resp) => {
				resp.data.data.forEach((item, i) => {
					if (item.pid == 0) {
						this.booktype.push({
							value: item.id,
							label: item.name
						})
					}
				})
				this.booktype.forEach((item, i) => {
					let x = []
					resp.data.data.forEach((y, i) => {
						if (y.pid > 0 && y.pid == item.value) {
							x.push({
								value: y.id,
								label: y.name,
							})
						}
					})
					item.children = x
					console.log(x);
				})
			})
		}
	}
</script>

<style>
</style>